package com.yaowk.utils.email;


import com.yaowk.common.api.BaseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import javax.mail.internet.MimeMessage;
import java.util.List;


@Component
public class EmailUtils {

    @Value("${spring.mail.username:}")
    private String sendFrom;

    @Autowired(required = false)
    private JavaMailSender mailSender;


    /**
     * 发送html格式邮件
     *
     * @param sendTo
     * @param content
     * @param title
     */
    public void sendHtmlMessage(String sendTo, String content, String title) {
        if (ObjectUtils.isEmpty(sendTo)) {
            throw new BaseException("邮件发送目标不能为空");
        }
        MimeMessage message = null;
        try {
            message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(sendFrom);
            helper.setTo(sendTo);
            helper.setSubject(title);
            helper.setText(content, true);
        } catch (Exception e) {
            e.printStackTrace();
        }

        mailSender.send(message);
    }

    /**
     * 发送html格式邮件
     *
     * @param sendTo
     * @param content
     * @param title
     */
    public void sendHtmlMessage(List<String> sendTo, String content, String title) {
        if (CollectionUtils.isEmpty(sendTo)) {
            throw new BaseException("邮件发送目标不能为空");
        }
        MimeMessage message = null;
        try {
            message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(sendFrom);
            helper.setTo(sendTo.toArray(new String[sendTo.size()]));
            helper.setSubject(title);
            helper.setText(content, true);
        } catch (Exception e) {
            e.printStackTrace();
        }

        mailSender.send(message);
    }

    /**
     * 发送简单邮件
     *
     * @param sendTo
     * @param content
     * @param title
     */
    public void sendSimpleMessage(String sendTo, String content, String title) {
        if (ObjectUtils.isEmpty(sendTo)) {
            throw new BaseException("邮件发送目标不能为空");
        }
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(sendFrom);//发送者.
        message.setTo(sendTo);//接收者.
        message.setSubject(title);//邮件主题.
        message.setText(content);//邮件内容.
        mailSender.send(message);
    }


    /**
     * 发送简单邮件
     *
     * @param sendTo
     * @param content
     * @param title
     */
    public void sendSimpleMessage(List<String> sendTo, String content, String title) {
        if (CollectionUtils.isEmpty(sendTo)) {
            throw new BaseException("邮件发送目标不能为空");
        }
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(sendFrom);//发送者.
        message.setTo(sendTo.toArray(new String[sendTo.size()]));//接收者.
        message.setSubject(title);//邮件主题.
        message.setText(content);//邮件内容.
        mailSender.send(message);
    }
}
